<template>
  <div>
    <div> <!-- the first row -->
      <AnalysisSection v-for="(section, index) in analysisData.sections"
                       v-model="analysisData.sections[index]"
                       :key="section.id"
                       :section="section"
                       @add="addCondition" @delete="deleteCondition" @input="change"/>
    </div>
    <div v-if="emptySet"> <!-- 区间重叠提示 -->
      <span style="font-size: 12px;color: #EC0000;letter-spacing: 0;">区间不存在，请检查！</span>
    </div>
    <div> <!-- the second row -->
      <el-input v-model="analysisData.explanation" placeholder="解析结果说明"></el-input>
    </div>
    <div style="position: relative;"> <!-- the third row -->
      <div class="inline-block">
        <ResultButtons v-model="analysisData.result" @input="change"></ResultButtons>
      </div>
      <div class="inline-block" style="position: absolute; top: 5px; right: 0;">
        <el-button plain type="danger" @click="deleteAnalysis">删除此条</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import AnalysisSection from "./AnalysisSection";
import ResultButtons from "./ResultButtons";
import CalculateInterval from "./CalculateInterval";

export default {
  name: "ResultAnalysis",
  components: {
    AnalysisSection,
    ResultButtons,
    CalculateInterval
  },
  props: ["value", 'idx'],
  data() {
    return {
      emptySet: false, // 计算出的区间是否为空
      analysisData: {}
    }
  },
  methods: {
    addCondition() {
      this.analysisData.sections.push({
        id: this.analysisData.sections[this.analysisData.sections.length -1].id + 1,
        relation: 0,
        condition: 0,
        value: 0
      })
      this.change()
    },
    deleteCondition(section) {
      this.analysisData.sections.splice(section.id, 1)
      this.change()
    },
    buildCondition: function (section, condition) { // 构建condition，所谓condition代表一个区间
      if (section.condition === 0) { // 大于等于
        condition.left = section.value
        condition.leftContain = true
        condition.right = "+infinity"
        condition.rightContain = false
      } else if (section.condition === 1) { // 大于
        condition.left = section.value
        condition.leftContain = false
        condition.right = "+infinity"
        condition.rightContain = false
      } else if (section.condition === 2) { // 等于
        condition.left = section.value
        condition.leftContain = true
        condition.right = section.value
        condition.rightContain = true
      } else if (section.condition === 3) { // 小于等于
        condition.left = "-infinity"
        condition.leftContain = false
        condition.right = section.value
        condition.rightContain = true
      } else if (section.condition === 4) { // 小于
        condition.left = "-infinity"
        condition.leftContain = false
        condition.right = section.value
        condition.rightContain = false
      }
    },
    change() {
      // 检查计算出的区间是否为空
      // 存储区间运算的中间计算结果
      // 最终要通过判断这个变量是否是空数组来判断区间计算的结果是否是空
      // 如果区间计算为空则会显示错误提示
      let tempSectionArray = [];

      for (let i = 0; i < this.analysisData.sections.length; i++) {
        let section = this.analysisData.sections[i] // 这是一条结果解析中的一个条件

        let condition = {}
        this.buildCondition(section, condition);

        if (i === 0) {
          tempSectionArray.push(condition)
        } else {
          let tempArray = []
          for (let j = 0; j < tempSectionArray.length; j++) {
            let resultCondition = CalculateInterval.calculate(tempSectionArray[j], section.relation, condition)

            // 更新tempSectionArray中的值
            if (section.relation === 0) { // and
              if (resultCondition instanceof Object) { // 计算的结果是一个区间对象
                if (resultCondition.left !== "null" && resultCondition.right !== "null") { // 如果不是空集
                  tempArray.push(resultCondition)
                }
              }
            } else if (section.relation === 1) { // or
              if (resultCondition instanceof Object) {
                if (resultCondition.left === "-infinity" && resultCondition.right === "+infinity") { // 如果是全集
                  tempArray = [resultCondition]
                } else {
                  tempArray.push(resultCondition)
                }
              } else if (resultCondition instanceof Array) {
                for (let k = 0; k < resultCondition.length; k++) {
                  if (CalculateInterval.compare(resultCondition[k], CalculateInterval.infinity) !== 0) { // 如果不是全集
                    tempArray.push(resultCondition[k])
                  } else {
                    tempArray = [CalculateInterval.infinity]
                  }
                }
              }
            }
          }
          tempSectionArray = tempArray
        }
      }

      this.emptySet = tempSectionArray.length === 0

      this.analysisData.isLegalInterval = !this.emptySet // 是否是合法的区间
      this.$emit("input", this.analysisData)
    },
    deleteAnalysis() {
      this.$emit("deleteAnalysis", this.idx)
    }
  },
  created() {
    this.analysisData = this.value
  }
}
</script>

<style scoped>
.inline-block {
  display: inline-block;
}
</style>
